<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>利用SpannableString富文本方式设置圆角标签背景 - smartzheng</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="smartzheng" /><meta name="description" content="项目中遇到一个需求,需要在商品标题加上标签,而标签是客户可以后台配置的,所以不是用的图片,而是用的文字.如下图: 众所周知,在Android中" /><meta name="keywords" content="Hugo, theme, even" />






<meta name="generator" content="Hugo 0.68.3 with theme even" />


<link rel="canonical" href="https://smartzheng.github.io/post/%E5%88%A9%E7%94%A8spannablestring%E5%AF%8C%E6%96%87%E6%9C%AC%E6%96%B9%E5%BC%8F%E8%AE%BE%E7%BD%AE%E5%9C%86%E8%A7%92%E6%A0%87%E7%AD%BE%E8%83%8C%E6%99%AF/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">


<link href="/dist/even.c2a46f00.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="利用SpannableString富文本方式设置圆角标签背景" />
<meta property="og:description" content="项目中遇到一个需求,需要在商品标题加上标签,而标签是客户可以后台配置的,所以不是用的图片,而是用的文字.如下图: 众所周知,在Android中" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://smartzheng.github.io/post/%E5%88%A9%E7%94%A8spannablestring%E5%AF%8C%E6%96%87%E6%9C%AC%E6%96%B9%E5%BC%8F%E8%AE%BE%E7%BD%AE%E5%9C%86%E8%A7%92%E6%A0%87%E7%AD%BE%E8%83%8C%E6%99%AF/" />
<meta property="article:published_time" content="2018-03-28T00:00:00+00:00" />
<meta property="article:modified_time" content="2018-03-28T00:00:00+00:00" />
<meta itemprop="name" content="利用SpannableString富文本方式设置圆角标签背景">
<meta itemprop="description" content="项目中遇到一个需求,需要在商品标题加上标签,而标签是客户可以后台配置的,所以不是用的图片,而是用的文字.如下图: 众所周知,在Android中">
<meta itemprop="datePublished" content="2018-03-28T00:00:00&#43;00:00" />
<meta itemprop="dateModified" content="2018-03-28T00:00:00&#43;00:00" />
<meta itemprop="wordCount" content="654">



<meta itemprop="keywords" content="" /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="利用SpannableString富文本方式设置圆角标签背景"/>
<meta name="twitter:description" content="项目中遇到一个需求,需要在商品标题加上标签,而标签是客户可以后台配置的,所以不是用的图片,而是用的文字.如下图: 众所周知,在Android中"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">smartzheng</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">Home</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">Archives</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">Tags</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">Categories</li>
      </a>
  </ul>
</nav>
  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">smartzheng</a>
</div>

<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">Home</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">Archives</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">Tags</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">Categories</a>
      </li>
  </ul>
</nav>
    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">利用SpannableString富文本方式设置圆角标签背景</h1>

      <div class="post-meta">
        <span class="post-time"> 2018-03-28 </span>
        <div class="post-category">
            <a href="/categories/android/"> Android </a>
            </div>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li></li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <h4 id="项目中遇到一个需求需要在商品标题加上标签而标签是客户可以后台配置的所以不是用的图片而是用的文字如下图">项目中遇到一个需求,需要在商品标题加上标签,而标签是客户可以后台配置的,所以不是用的图片,而是用的文字.如下图:</h4>
<p><img src="https://upload-images.jianshu.io/upload_images/2983970-c45ecdf7ffd29eb2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/340" alt=""></p>
<h4 id="众所周知在android中view都是呈方形布置的所以如果标签和文字如果不是同一个view那么如果文字换行就会出现标签和textview分别在左右两边的效果">众所周知,在Android中,View都是呈方形布置的,所以如果标签和文字如果不是同一个View,那么如果文字换行,就会出现标签和TextView分别在左右两边的效果:</h4>
<p><img src="https://upload-images.jianshu.io/upload_images/2983970-276b1e708bb147d1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/340" alt=""></p>
<h4 id="经过思考和查阅资料发现可以用spannablestring设置背景并通过重写replacementspan替换原来的backgroundcolorspan来实现圆角标签">经过思考和查阅资料,发现可以用SpannableString设置背景,并通过重写ReplacementSpan替换原来的BackgroundColorSpan来实现圆角标签.</h4>
<h4 id="下面是代码">下面是代码:</h4>
<p>Activity: (goodsTags为标签文字集合,即上图的&quot;促&rdquo;,&ldquo;九&rdquo;,&ldquo;八&rdquo;&hellip;goodsName为商品名字</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-java" data-lang="java"><span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">0</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">goodsTags</span><span class="o">.</span><span class="na">size</span><span class="o">();</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
	<span class="c1">//为了显示效果在每个标签文字前加两个空格,后面加三个空格(前两个和后两个填充背景,最后一个作标签分割)
</span><span class="c1"></span>    <span class="n">goodsName</span><span class="o">.</span><span class="na">insert</span><span class="o">(</span><span class="n">0</span><span class="o">,</span> <span class="s">&#34;  &#34;</span> <span class="o">+</span> <span class="n">goodsTags</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">i</span><span class="o">).</span><span class="na">getTags_name</span><span class="o">()</span> <span class="o">+</span> <span class="s">&#34;   &#34;</span><span class="o">);</span>
    <span class="kt">int</span> <span class="n">start</span> <span class="o">=</span> <span class="n">0</span><span class="o">;</span>
    <span class="kt">int</span> <span class="n">end</span> <span class="o">=</span> <span class="n">5</span><span class="o">;</span>
    <span class="c1">//稍微设置标签文字小一点
</span><span class="c1"></span>    <span class="n">goodsName</span><span class="o">.</span><span class="na">setSpan</span><span class="o">(</span><span class="k">new</span> <span class="n">RelativeSizeSpan</span><span class="o">(</span><span class="n">0</span><span class="o">.</span><span class="na">9f</span><span class="o">),</span> <span class="n">start</span><span class="o">,</span> <span class="n">end</span><span class="o">,</span> <span class="n">Spanned</span><span class="o">.</span><span class="na">SPAN_EXCLUSIVE_EXCLUSIVE</span><span class="o">);</span>
    <span class="c1">//设置圆角背景
</span><span class="c1"></span>    <span class="n">goodsName</span><span class="o">.</span><span class="na">setSpan</span><span class="o">(</span><span class="k">new</span> <span class="n">RoundBackgroundColorSpan</span><span class="o">(</span><span class="n">Color</span><span class="o">.</span><span class="na">parseColor</span><span class="o">(</span><span class="s">&#34;#&#34;</span> <span class="o">+</span> <span class="n">goodsTags</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">i</span><span class="o">).</span><span class="na">getTags_color</span><span class="o">()),</span><span class="n">Color</span><span class="o">.</span><span class="na">WHITE</span><span class="o">),</span> <span class="n">start</span><span class="o">,</span> <span class="n">end</span><span class="o">,</span> <span class="n">Spanned</span><span class="o">.</span><span class="na">SPAN_EXCLUSIVE_EXCLUSIVE</span><span class="o">);</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><p>自定义RoundBackgroundColorSpan:</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-java" data-lang="java"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">RoundBackgroundColorSpan</span> <span class="kd">extends</span> <span class="n">ReplacementSpan</span> <span class="o">{</span>
    <span class="kd">private</span> <span class="kt">int</span> <span class="n">bgColor</span><span class="o">;</span>
    <span class="kd">private</span> <span class="kt">int</span> <span class="n">textColor</span><span class="o">;</span>
    <span class="kd">public</span> <span class="nf">RoundBackgroundColorSpan</span><span class="o">(</span><span class="kt">int</span> <span class="n">bgColor</span><span class="o">,</span> <span class="kt">int</span> <span class="n">textColor</span><span class="o">)</span> <span class="o">{</span>
        <span class="kd">super</span><span class="o">();</span>
        <span class="k">this</span><span class="o">.</span><span class="na">bgColor</span> <span class="o">=</span> <span class="n">bgColor</span><span class="o">;</span>
        <span class="k">this</span><span class="o">.</span><span class="na">textColor</span> <span class="o">=</span> <span class="n">textColor</span><span class="o">;</span>
    <span class="o">}</span>
    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="kt">int</span> <span class="nf">getSize</span><span class="o">(</span><span class="n">Paint</span> <span class="n">paint</span><span class="o">,</span> <span class="n">CharSequence</span> <span class="n">text</span><span class="o">,</span> <span class="kt">int</span> <span class="n">start</span><span class="o">,</span> <span class="kt">int</span> <span class="n">end</span><span class="o">,</span> <span class="n">Paint</span><span class="o">.</span><span class="na">FontMetricsInt</span> <span class="n">fm</span><span class="o">)</span> <span class="o">{</span>
   		<span class="c1">//设置宽度为文字宽度加16dp
</span><span class="c1"></span>        <span class="k">return</span> <span class="o">((</span><span class="kt">int</span><span class="o">)</span><span class="n">paint</span><span class="o">.</span><span class="na">measureText</span><span class="o">(</span><span class="n">text</span><span class="o">,</span> <span class="n">start</span><span class="o">,</span> <span class="n">end</span><span class="o">)+</span><span class="n">Util</span><span class="o">.</span><span class="na">px2Dp</span><span class="o">(</span><span class="n">16</span><span class="o">));</span>
    <span class="o">}</span>

    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">draw</span><span class="o">(</span><span class="n">Canvas</span> <span class="n">canvas</span><span class="o">,</span> <span class="n">CharSequence</span> <span class="n">text</span><span class="o">,</span> <span class="kt">int</span> <span class="n">start</span><span class="o">,</span> <span class="kt">int</span> <span class="n">end</span><span class="o">,</span> <span class="kt">float</span> <span class="n">x</span><span class="o">,</span> <span class="kt">int</span> <span class="n">top</span><span class="o">,</span> <span class="kt">int</span> <span class="n">y</span><span class="o">,</span> <span class="kt">int</span> <span class="n">bottom</span><span class="o">,</span> <span class="n">Paint</span> <span class="n">paint</span><span class="o">)</span> <span class="o">{</span>
        <span class="kt">int</span> <span class="n">originalColor</span> <span class="o">=</span> <span class="n">paint</span><span class="o">.</span><span class="na">getColor</span><span class="o">();</span>
        <span class="n">paint</span><span class="o">.</span><span class="na">setColor</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">bgColor</span><span class="o">);</span>
        <span class="c1">//画圆角矩形背景
</span><span class="c1"></span>        <span class="n">canvas</span><span class="o">.</span><span class="na">drawRoundRect</span><span class="o">(</span><span class="k">new</span> <span class="n">RectF</span><span class="o">(</span><span class="n">x</span><span class="o">,</span>
                <span class="n">top</span><span class="o">+</span> <span class="n">Util</span><span class="o">.</span><span class="na">px2Dp</span><span class="o">(</span><span class="n">3</span><span class="o">),</span>
                <span class="n">x</span> <span class="o">+</span> <span class="o">((</span><span class="kt">int</span><span class="o">)</span> <span class="n">paint</span><span class="o">.</span><span class="na">measureText</span><span class="o">(</span><span class="n">text</span><span class="o">,</span> <span class="n">start</span><span class="o">,</span> <span class="n">end</span><span class="o">)+</span> <span class="n">Util</span><span class="o">.</span><span class="na">px2Dp</span><span class="o">(</span><span class="n">16</span><span class="o">)),</span>
                <span class="n">bottom</span><span class="o">-</span><span class="n">Util</span><span class="o">.</span><span class="na">px2Dp</span><span class="o">(</span><span class="n">1</span><span class="o">)),</span>

                <span class="n">Util</span><span class="o">.</span><span class="na">px2Dp</span><span class="o">(</span><span class="n">4</span><span class="o">),</span>
                <span class="n">Util</span><span class="o">.</span><span class="na">px2Dp</span><span class="o">(</span><span class="n">4</span><span class="o">),</span>
                <span class="n">paint</span><span class="o">);</span>
        <span class="n">paint</span><span class="o">.</span><span class="na">setColor</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">textColor</span><span class="o">);</span>
        <span class="c1">//画文字,两边各增加8dp
</span><span class="c1"></span>        <span class="n">canvas</span><span class="o">.</span><span class="na">drawText</span><span class="o">(</span><span class="n">text</span><span class="o">,</span> <span class="n">start</span><span class="o">,</span> <span class="n">end</span><span class="o">,</span> <span class="n">x</span><span class="o">+</span><span class="n">Util</span><span class="o">.</span><span class="na">px2Dp</span><span class="o">(</span><span class="n">8</span><span class="o">),</span> <span class="n">y</span><span class="o">,</span> <span class="n">paint</span><span class="o">);</span>
        <span class="c1">//将paint复原
</span><span class="c1"></span>        <span class="n">paint</span><span class="o">.</span><span class="na">setColor</span><span class="o">(</span><span class="n">originalColor</span><span class="o">);</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div>
    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">smartzheng</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2018-03-28
        
    </span>
  </p>
  
  
</div>
<footer class="post-footer">
      
      <nav class="post-nav">
        <a class="prev" href="/post/%E5%88%A9%E7%94%A8python%E5%AE%9E%E7%8E%B0android%E6%9B%BF%E6%8D%A2%E5%86%85%E5%AE%B9%E6%89%B9%E9%87%8F%E6%89%93%E5%8C%85/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">利用Python实现Android替换内容批量打包</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/bilibili%E5%BC%80%E6%BA%90%E5%BA%93ijkpalyer%E7%9A%84%E9%9B%86%E6%88%90%E5%92%8C%E4%BD%BF%E7%94%A8/">
            <span class="next-text nav-default">bilibili开源库ijkPalyer的集成和使用</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:smartzheng1993@email.com" class="iconfont icon-email" title="email"></a>
      <a href="https://github.com/smartzheng" class="iconfont icon-github" title="github"></a>
      <a href="https://weibo.com/2062336885/profile?topnav=1&amp;wvr=6" class="iconfont icon-weibo" title="weibo"></a>
      <a href="https://www.zhihu.com/people/liu-xia-hui-89-70" class="iconfont icon-zhihu" title="zhihu"></a>
      <a href="https://www.douban.com/people/smartzheng/" class="iconfont icon-douban" title="douban"></a>
  <a href="https://smartzheng.github.io/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  

  <span class="copyright-year">
    &copy; 
    2017 - 
    2020
    
  </span>
</div>
    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>
<script type="text/javascript" src="/dist/even.26188efa.min.js"></script>








</body>
</html>
